package com.bidevalution.business.function;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
/**
 * 在数组中去除最前面几个和最后面几个数据
 * @author daizhifeng   
 * @date 2019年10月10日 下午9:38:17 
 * @version V2.0
 */
public class RemoveLimitOfArrayFunction extends AbstractFunction{
	private Logger logger = LogManager.getLogger(getClass());
	public RemoveLimitOfArrayFunction(FunctionInitializer functionInitializer) {
		super();
	}
	/**
	 * 在数组中去除最前面几个和最后面几个数据
	 * <p>Description: </p>
	 * @param env
	 * @param arg1 数组
	 * @param arg2 前面几位
	 * @param arg3 后面几位
	 * @return 返回数组
	 * @see com.googlecode.aviator.runtime.function.AbstractFunction#call(java.util.Map, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject, com.googlecode.aviator.runtime.type.AviatorObject)
	 */
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
		String paramValue = FunctionUtils.getStringValue(arg1, env);
		List<String> priceList = new LinkedList<String>(Arrays.asList(paramValue.split(",")));
		System.out.println(arg1.getValue(env).toString()+"RemoveLimitOfArrayFunction"+arg2.getValue(env).toString());
		Integer minLimit = Integer.valueOf(arg2.getValue(env).toString());//去掉最前面的数组范围
		Integer maxLimit = Integer.valueOf(arg3.getValue(env).toString());//去掉最后面的数组范围
		if(priceList.size()>=(minLimit+maxLimit)) {
			for(int i = 0;i<minLimit;i++) {
				priceList.remove(0);
			}
			for(int i = 0;i<maxLimit;i++) {
				priceList.remove(priceList.size() - 1);
			}
		}
		String result = StringUtils.collectionToDelimitedString(priceList, ",");
		logger.info("removeLimitOfArray:"+result);
		return new AviatorString(result);
	}
	@Override
	public String getName() {
		return "removeLimitOfArray";
	}

}
